<template>
    <div id= 'login'>
        <input type='text' name='phone' v-model="phone">
        <input type='password' name='password' v-model='password'>
        <input type='text' name='token' v-bind:value='tokens'>
        <button @click="loginclick">提交</button>
        <!-- <loading v-bind:status="load"></loading> -->
        <!-- <alerts v-bind:tip="content" :alertStatus="alertType"></alerts> -->
    </div>
</template>
<style scoped>

</style>
<script>
import { setTimeout } from 'timers'
export default {
    data () {
        return {
            phone: '13609847311',
            password: '111111',
            token: this.$store.state.token,
            load: false, // loading图画的显示
            content: '', // 提示的内容
            alertType: false //是否显示提示
        }
    },
    computed: {
        tokens () {
            return this.$store.state.token
        }
    },
    methods: {
        loginclick () {
            if(this.phone != '' && this.password != ''){
                this.$store.commit('showLoading') // loading加载
                this.$http.post('api/v1.0/login', {phone: this.phone, password: this.password}).then((res) =>{
                    if(res != ''){
                        console.log(res.data.token)
                        this.$store.commit('token', res.data.token)
                        this.$store.commit('hideLoading')
                        this.$store.commit('showAlert')
                        this.$store.commit('changeAlertContent', '请求成功')
                        setTimeout(()=>{this.$store.commit('hideAlert')},3000)
                    }else{
                        this.$store.commit('hideLoading')
                        this.$store.commit('showAlert')
                        this.$store.commit('changeAlertContent', '请求失败')
                        setTimeout(()=>{this.$store.commit('hideAlert')},3000)
                    }
                })
            }else{
                this.$store.commit('showAlert')
                this.$store.commit('changeAlertContent', '账号密码不能为空')
                setTimeout(()=>{this.$store.commit('hideAlert')},3000)
            }
        }
    }
}
</script>

